<?php

class Librarian_AuthorController extends Zend_Controller_Action
{

    public function indexAction ()
    {
        $q = trim($this->getRequest()->getParam('q', ''));
        
        if ($q) {
            
            $authors = Model_Author::find(array('q' => $q));
            
        } else {
            
            $authors = Model_Author::getAll();
        }
        
        $this->view->assign('q', $q);
        $this->view->assign('authors', $authors);
    }

    public function createAction ()
    {
        if ($this->getRequest()->isPost()) {
            
            $response = array(
                    'status' => 0,
                    'message' => 'Unknown error...'
            );
            
            $params = $this->getRequest()->getPost();
            
            if (! $params['Name'] || strlen($params['Name']) > 200) {
                
                $response['message'] = 'Tên chuyên mục phải từ 1 - 200 ký tự.';
            } else {
                
                try {
                    
                    if (Model_Author::create($params)) {
                        $response['status'] = 1;
                        $response['message'] = 'Success';
                    }
                } catch (Zend_Db_Exception $e) {
                    $response['message'] = $e->getMessage();
                }
            }
            
            $this->_helper->json($response);
        }
    }

    public function editAction ()
    {
        if ($this->getRequest()->isPost()) {
            
            $response = array(
                    'status' => 0,
                    'message' => 'Unknown error...'
            );
            
            $params = $this->getRequest()->getPost();
            
            if (! $params['Name'] || strlen($params['Name']) > 200) {
                
                $response['message'] = 'Tên chuyên mục phải từ 1 - 200 ký tự.';
            } elseif (! $params['AuthorID']) {
                
                $response['message'] = 'Bạn chưa chọn tác giả để chính sửa.';
            } else {
                
                try {
                    
                    Model_Author::edit($params);
                    $response['status'] = 1;
                    $response['message'] = 'Success';
                } catch (Zend_Db_Exception $e) {
                    $response['message'] = $e->getMessage();
                }
            }
            
            $this->_helper->json($response);
        }
        
        $params = $this->getRequest()->getParams();
        
        if (! intval($params['author_id'])) {
            $this->_redirect('/librarian/author');
        }
        
        $author = Model_Author::get($params);
        
        if (empty($author)) {
            $this->_redirect('/librarian/author');
        }
        
        $this->view->assign('author', $author);
    }

    public function deleteAction ()
    {
        if ($this->getRequest()->isPost()) {
            
            $response = array(
                    'status' => 0,
                    'message' => 'Unknown error...'
            );
            
            $params = $this->getRequest()->getPost();
            
            if (! intval($params['authorID'])) {
                
                $response['message'] = 'Bạn chưa chọn tác giả để xoá.';
            } else {
                try {
                    
                    if (Model_Author::delete($params)) {
                        $response['status'] = 1;
                        $response['message'] = 'Success';
                    }
                } catch (Zend_Db_Exception $e) {
                    $response['message'] = $e->getMessage();
                }
            }
            
            $this->_helper->json($response);
        }
    }
}
